<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>超期物料重检表</title>
    <link rel="stylesheet" href="/3nod_web/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="/3nod_web/css/formSelects-v4.css" />
</head>
<body>
<div id="content">
    <h1>超期物料重检表&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <button class="layui-btn layui-btn layui-btn-sm" onclick="toExport();">导出</button>
    </h1>
    <form class="layui-form" action="javascript:;" id="myform">
        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-inline">
                <label class="layui-form-label">据点</label>
                <div class="layui-input-inline" style="width:168px;">
                    <input type="text" name="storerkey" id="storerkey" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">品号</label>
                <div class="layui-input-inline" style="width:168px;">
                    <input type="text" name="sku" id="sku" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">逻辑仓</label>
                <div class="layui-input-inline" style="width:168px;">
                    <input type="text" name="lottable01" id="lottable01" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">供应商</label>
                <div class="layui-input-inline" style="width:168px;">
                    <input type="text" name="lottable02" id="lottable02" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">存储期限</label>
                <div class="layui-input-inline" style="width:168px;">
                    <input type="text" name="default3" id="default3" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">保质期限</label>
                <div class="layui-input-inline" style="width:168px;">
                    <input type="text" name="default2" id="default2" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class=" layui-inline">
                <label class="layui-form-label">超期类别</label>
                <div class="layui-input-inline " style="width: 130px;">
                    <select id="type" lay-search name="type"  lay-filter="interest" lay-submit>
                        <option value="">请选择</option>
                        <option value="default3">超存储期限</option>
                        <option value="default2">超保质期</option>
                    </select>
                </div>
            </div>

            <div class=" layui-inline">
                <label class="layui-form-label">机型</label>
                <div class="layui-input-inline " style="width: 300px;">
                    <select id="modelKey" lay-search name="modelKey"   xm-select="select1">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>

        </div>
    </form>
    <table class="layui-hide" id="demo" lay-filter="demo"></table>
</div>

<script src="/3nod_web/layui/layui.js" charset="utf-8"></script>
<script src="/3nod_web/js/jquery-1.8.3.js" charset="utf-8"></script>
<script src="/3nod_web/js/formSelects-v4.min.js" charset="utf-8"></script>

<script>
    var layer = null;
    var form = null;
    layui.use(['layer', 'form'], function () {
        layer = layui.layer;
        form = layui.form;
        // 超期类别
        form.on('select(interest)', function (data) {

        });

        getModelKey();
        form.render();
    });

    //机型下拉框
    function getModelKey(){
        $.get("/3nod_web/report/getModelKey?shipper=",function (res) {
            for(var i =0;i<res.data.length;i++){
                $("#modelKey").append("<option value=\""+res.data[i].modelKey+"\">"+res.data[i].modelName+"</option>");
            }
            //重新渲染
            layui.formSelects.render("select1");
        });
    }

    layui.use('table', function () {
        var table = layui.table;
        //方法级渲染
        table.render({
            elem: '#demo'
            , url: '/3nod_web/report/getOverdueMaterialList/'
            , cellMinWidth: 120
            , cols: [
                [
                    {field: 'storerkey', title: '据点', sort: true, fixed: true}
                    , {field: 'modelname', title: '机型', sort: true, fixed: true}
                    , {field: 'sku', title: '品号', sort: true, fixed: true}
                    , {field: 'descr', title: '品名', sort: true}
                    , {field: 'default6', title: '规格', sort: true}
                    , {field: 'lottable01', title: '逻辑仓', sort: true}
                    , {field: 'loc', title: '库位', sort: true}
                    , {field: 'qty', title: '数量', sort: true}
                    , {field: 'lottable02', title: '供应商', sort: true}
                    , {field: 'lottable04', title: '制造日期', sort: true}
                    , {field: 'default2', title: '保质期限', sort: true}
                    , {field: 'supDefault2', title: '超保质期限天数', sort: true, width: '160'}
                    , {field: 'default3', title: '存储期限', sort: true, width: '160'}
                    , {field: 'extradays', title: '超存储期限天数', sort: true, width: '160'}
                    , {field: 'reCheckDate', title: '重检日期', sort: true, edit: 'text', width: '160'}
                    , {field: 'reDefault2', title: '重检后保质期', sort: true, width: '160'}
                ]
            ]
            , done: function (res, curr, count) {

            }
            , id: 'testReload'
            , height: 'full-160'
            , page: true
            , limits: [20, 50, 100, 200, 500, 1000, 2000, 3000, 4000, 5000]
            , limit: 20
        });


        var $ = layui.$, active = {
            reload: function () {
                var storerkey = $('#storerkey');
                var modelKey = layui.formSelects.value('select1', 'valStr');
                var sku = $('#sku');
                var lottable01 = $('#lottable01');
                var lottable02 = $('#lottable02');
                var default3 = $('#default3');
                var default2 = $('#default2');
                var type=$("#type option:selected");

                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        storerkey: storerkey.val(),
                        modelKey: modelKey,
                        sku: sku.val(),
                        lottable01: lottable01.val(),
                        lottable02: lottable02.val(),
                        default3: default3.val(),
                        default2: default2.val(),
                        type: type.val()
                    }
                }, 'data');
            }
        };
        //按回车提交
        $(".layui-input-inline").keydown(function (e) {//当按下按键时
            if (e.which == 13) {//.which属性判断按下的是哪个键，回车键的键位序号为13
                // $('button.search123').trigger("click");//触发搜索按钮的点击事件
                active['reload'] ? active['reload'].call(this) : '';
            }
        });


        //检验项目编辑
        table.on('edit(demo)', function (obj) {

            var $ = layui.$;
            var value = obj.value //得到修改后的值
                , data = obj.data;//得到所在行所有键值
            var reg = /^\d{4}-\d{2}-\d{2}$/;
            var re = new RegExp(reg);
            if (value !== '') {
                if (!re.test(value)) {
                    layer.alert("日期格式不正确：正确格式为 yyyy-MM-dd");
                    return false;
                }
            }

            $.ajax({
                url: "/3nod_web/sku/update", // ajax请求路径
                type: 'POST',
                contentType: 'application/json; charset=UTF-8',
                async: false,
                dataType: 'text',
                data: JSON.stringify(data),
                success: function () {
                    var seconds = (Date.parse(data.lottable04) - Date.parse(data.reCheckDate)) / 1000;
                    var days = Math.floor(seconds / (3600 * 24));
                    var reDefault2 = parseInt(days) + parseInt(data.default2);
                    layer.msg('重检日期更改为：' + reDefault2);
                    if (data.reCheckDate == '') {
                        obj.update({reDefault2: ''});
                    } else {
                        obj.update({reDefault2: reDefault2});
                    }
                    // active['reload'] ? active['reload'].call(this) : '';
                },
                error: function () {
                    layer.alert("更新失败");
                }
            });
        });

    });

    //导出
    function toExport() {
        var storerkey = $('#storerkey').val();
        var sku = $('#sku').val();
        var lottable01 = $('#lottable01').val();
        var lottable02 = $('#lottable02').val();
        var default3 = $('#default3').val();

        location.href = '/3nod_web/exportOverdueMaterial?storerkey=' + storerkey + "&lottable01=" + lottable01 + "&lottable02=" + lottable02
            + "&sku=" + sku + "&default3=" + default3;
    }

</script>


</body>
</html>